package com.itheima.dao;

import com.itheima.bean.pojo.User;
import com.itheima.bean.vo.AddUser;
import com.itheima.bean.vo.UpdateUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {

    /**
     * 添加用户数据
     * @param user
     * @return
     */
    @Insert("insert into t_user(username,password,remark,email,createTime) " +
            "values (#{username},#{password},#{remark},#{email},now())")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    public int addUser(AddUser user);

    /**
     * 添加用户角色
     * @param userid
     * @param roleid
     * @return
     */
    @Insert("insert into t_user_role(user_id, role_id) values (#{userid},#{roleid})")
    public int addUserRole(@Param("userid") Integer userid,
                           @Param("roleid") Integer roleid);



    /**
     * 分页查询： 用户信息及用户角色信息
     *
     * @param index
     * @param pageSize
     * @return
     */
    public List<User> findUserByPage(@Param("index") Integer index ,
                                     @Param("pageSize") Integer pageSize);

    /**
     * 分页查询：统计查询总记录数
     * @return
     */
    @Select("select count(*) from t_user")
    public int findCount();

    @Delete("delete from t_user_role where user_id= #{id}")
    boolean  deleteUserRoleById(@Param("id")Integer id);

    @Delete("delete from t_user where id= #{id}")
    boolean  deleteUser(Integer id);

    //更新用户表
    @Update("update t_user set username=#{username},password=#{password},email=#{email} where id = #{id}")
    void updateUser(UpdateUser user);

    //删除中间表
    @Delete("delete from t_user_role where user_id =#{uid}")
    void deleteUser_Role(@Param("uid") Integer uid);
}
